<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true">
      <el-form-item :label="t('reviewApplyDetail.fields.nickName')" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          :placeholder="t('reviewApplyDetail.fields.nickName')"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
          :disabled="advancedSearching"
        />
      </el-form-item>
      <el-form-item :label="t('reviewApplyDetail.fields.result')" prop="result">
        <el-select
          v-model="queryParams.result"
          class="!w-120px"
          clearable
          :placeholder="t('reviewApplyDetail.fields.result')"
        >
          <el-option :label="t('reviewApplyDetail.index.passed')" :value="1" />
          <el-option :label="t('reviewApplyDetail.index.unPassed')" :value="-1" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery" :disabled="advancedSearching"
          ><Icon icon="ep:search" class="mr-5px" />{{ t('common.search') }}</el-button
        >
        <el-button @click="resetQuery" :disabled="advancedSearching"
          ><Icon icon="ep:refresh" class="mr-5px" />{{ t('common.reset') }}</el-button
        >
        <el-button type="success" plain @click="handleExport" :loading="exportLoading">
          <Icon icon="ep:download" class="mr-5px" />
          {{ t('action.export') }}
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <vxe-toolbar ref="toolbarRef" custom />
    <vxe-table
      id="iReviewDetail-table"
      ref="tableRef"
      v-loading="loading"
      :data="list"
      stripe
      :custom-config="vxeTableConfig"
      :column-config="{ resizable: true }"
      :size="vxeTableSize"
    >
      <vxe-column :title="t('common.index')" type="seq" field="seq" width="60" align="center" />
      <!-- 姓名 -->
      <vxe-column
        :title="t('reviewApplyDetail.fields.nickName')"
        field="nickName"
        align="center"
        show-header-overflow
        show-overflow
        min-width="50"
      />
      <!-- 工号 -->
      <vxe-column
        :title="t('reviewApplyDetail.fields.dsgh')"
        field="dsgh"
        align="center"
        show-header-overflow
        show-overflow
        min-width="50"
      />
      <!-- 部门名称 -->
      <vxe-column
        :title="t('reviewApplyDetail.fields.deptName')"
        field="deptName"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <!-- 性别 -->
      <vxe-column
        :title="t('reviewApplyDetail.fields.gender')"
        field="gender"
        align="center"
        show-header-overflow
        show-overflow
        min-width="50"
      >
        <template #default="{ row }">
          <span v-if="row.gender === '1'">男</span>
          <span v-if="row.gender === '2'">女</span>
        </template>
      </vxe-column>
      <!-- 生日 -->
      <vxe-column
        :title="t('reviewApplyDetail.fields.birthday')"
        field="birthday"
        align="center"
        show-header-overflow
        show-overflow
        min-width="80"
      />
      <!-- 所在课题组 -->
      <vxe-column
        :title="t('reviewApplyDetail.fields.researchGroup')"
        field="researchGroup"
        align="center"
        show-header-overflow
        show-overflow
        min-width="80"
      />
      <!-- 技术职务 -->
      <vxe-column
        :title="t('reviewApplyDetail.fields.technicalPosition')"
        field="technicalPosition"
        align="center"
        show-header-overflow
        show-overflow
        min-width="80"
      />
      <!-- 招生专业 -->
      <vxe-column
        :title="t('reviewApplyDetail.fields.enrollmentMajor')"
        field="enrollmentMajor"
        align="center"
        show-header-overflow
        show-overflow
        min-width="80"
      />
      <!-- 最高学位 -->
      <vxe-column
        :title="t('reviewApplyDetail.fields.highestDegree')"
        field="highestDegree"
        align="center"
        show-header-overflow
        show-overflow
        min-width="80"
      />
      <!-- 研究方向 -->
      <vxe-column
        :title="t('reviewApplyDetail.fields.researchDirection')"
        field="researchDirection"
        align="center"
        show-header-overflow
        show-overflow
        min-width="80"
      />
      <!-- 个人总经费 -->
      <vxe-column
        :title="t('reviewApplyDetail.fields.totalFunds')"
        field="totalFunds"
        align="center"
        show-header-overflow
        show-overflow
        min-width="120"
      />
      <!-- 主导师学生数 -->
      <vxe-column
        :title="t('reviewApplyDetail.fields.totalStudents')"
        field="totalStudents"
        align="center"
        show-header-overflow
        show-overflow
        min-width="120"
      />
      <!-- 评委人数 -->
      <vxe-column
        :title="t('reviewApplyDetail.fields.reviewCount')"
        field="reviewCount"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <!-- 同意人数 -->
      <vxe-column
        :title="t('reviewApplyDetail.fields.agreeCount')"
        field="agreeCount"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <!-- 不同意人数 -->
      <vxe-column
        :title="t('reviewApplyDetail.fields.disagreeCount')"
        field="disagreeCount"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <!-- 未投票人数 -->
      <vxe-column
        :title="t('reviewApplyDetail.fields.notVoteCount')"
        field="notVoteCount"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <!-- 评审结果 -->
      <vxe-column
        :title="t('reviewApplyDetail.fields.result')"
        field="result"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      >
        <template #default="scope">
          <span v-if="scope.row.result">{{
            scope.row.result === 1
              ? t('reviewApplyDetail.index.passed')
              : t('reviewApplyDetail.index.unPassed')
          }}</span>
          <span v-else>{{ t('reviewApplyDetail.index.unfinished') }}</span>
        </template>
      </vxe-column>
      <vxe-column
        :title="t('common.operate')"
        field="operator"
        align="center"
        fixed="right"
        width="140"
      >
        <template #default="{ row }">
          <el-button link type="primary" @click="handleDetail(row.id)">
            {{ t('reviewApplyDetail.index.operate.detail') }}
          </el-button>
          <el-button link type="primary" @click="handleOpenVoteDetail(row)">
            {{ t('reviewApplyDetail.index.operate.voteDetail') }}
          </el-button>
        </template>
      </vxe-column>
    </vxe-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 投票明细弹窗 -->
  <ReviewVoteDetail ref="reviewVoteDetailRef" :batch-id="batchId" />
  <!-- 申请信息详情弹窗 -->
  <QualificationReviewApplyDetail ref="qualificationReviewApplyDetailRef" />
</template>

<script setup lang="ts">
import download from '@/utils/download'
import type { VxeTablePropTypes, VxeToolbarInstance, VxeTableInstance } from 'vxe-table'
import { useAppStore } from '@/store/modules/app'
import ReviewVoteDetail from '@/views/recruit/reviewbatch/components/ReviewVoteDetail.vue'
import {
  QualificationReviewDetailApi,
  ReviewDetailRespVO
} from '@/api/recruit/qualificationReviewDetail'
import QualificationReviewApplyDetail from '@/views/recruit/components/QualificationReviewApplyDetail.vue'
import { BaseInfoVO } from '@/api/recruit/reviewApply/types'

/** 资审申请明细 列表 */
defineOptions({ name: 'ReviewApplicationDetail' })

const route = useRoute() // 路由
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const toolbarRef = ref<VxeToolbarInstance>()
const tableRef = ref<VxeTableInstance>()
const reviewVoteDetailRef = ref<InstanceType<typeof ReviewVoteDetail>>()
const qualificationReviewApplyDetailRef = ref<InstanceType<typeof QualificationReviewApplyDetail>>()

const loading = ref(true) // 列表的加载中
const list = ref<ReviewDetailRespVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const advancedSearching = ref(false) // 高级搜索
const queryParams = reactive({
  batchId: '',
  pageNo: 1,
  pageSize: 10,
  nickName: undefined,
  result: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

const vxeTableConfig: VxeTablePropTypes.CustomConfig = {
  storage: true,
  mode: 'drawer',
  checkMethod({ column }) {
    return !['seq', 'operator'].includes(column.field)
  }
}

const appStore = useAppStore()
const vxeTableSize = computed(() => appStore.getCurrentSizeVxe)

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await QualificationReviewDetailApi.getReviewDetailPage(queryParams)
    list.value = data.list.map((item) => {
      try {
        const baseInfo = JSON.parse(item.baseInformation) as BaseInfoVO
        return {
          ...item,
          ...baseInfo
        }
      } catch {
        return item
      }
    })
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  advancedSearching.value = false
  ;(queryParams as any).advanced = undefined
  handleQuery()
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await QualificationReviewDetailApi.exportReviewDetail(batchId.value)
    download.excel(data, `招生资审查申请明细${queryParams.batchId}.xlsx`)
  } finally {
    exportLoading.value = false
  }
}

// 打开投票明细弹窗
const handleOpenVoteDetail = (row: ReviewDetailRespVO) => {
  reviewVoteDetailRef.value?.open(row.id)
}

// 打开申请信息详情弹窗
const handleDetail = (id: string) => {
  qualificationReviewApplyDetailRef.value?.open(id)
}

//
const batchId = ref('')
/** 初始化 **/
onMounted(() => {
  batchId.value = route.params.batchId as string
  queryParams.batchId = batchId.value
  getList()

  const table = tableRef.value
  const toolbar = toolbarRef.value
  if (table && toolbar) {
    table.connect(toolbar)
  }
})
</script>
